﻿using KBWebApi.Common;
using KBWebApi.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace KBWebApi.Bll
{
    public class WorkStatusBll
    {
        Dal.WorkStatusDal workStatusDal = new Dal.WorkStatusDal();

        public async Task<ActionResult> NewOrUpdate(WorkStatusModel workStatus)
        {
            if (await Task.Run(() => Dal.WorkStatusDal.IsExists(workStatus)))
            {
                try
                {
                    var result = await Task.Run(() => workStatusDal.Update(workStatus));
                    if (result > 0)
                    {
                        return new JsonResult(new
                        {
                            code = 20000,
                            message = "更新成功"
                        });
                    }
                    else
                    {
                        return new JsonResult(new
                        {
                            code = 20002,
                            message = "更新失败"
                        });
                    }
                }
                catch (Exception ex)
                {
                    WriteLog.Writelog("WorkStatus.Update=>" + ex.ToString());
                    return new JsonResult(new
                    {
                        code = 20001,
                        message = "发生错误"
                    });
                }
            }
            else
            {
                try
                {
                    workStatus.CreateDate = DateTime.Now;
                    var result = await Task.Run(() => workStatusDal.NewKBInput(workStatus));
                    if (result > 0)
                    {
                        return new JsonResult(new
                        {
                            code = 20000,
                            message = "新增成功"
                        });
                    }
                    else
                    {
                        return new JsonResult(new
                        {
                            code = 20002,
                            message = "新增失败"
                        });
                    }
                }
                catch (Exception ex)
                {
                    WriteLog.Writelog("WorkStatus.New=>" + ex.ToString());
                    return new JsonResult(new
                    {
                        code = 20001,
                        message = "发生错误"
                    });
                }
            }
        }

        public async Task<List<WorkStatusModel>> GetAllList()
        {
            try
            {
                var result = await Task.Run(() => Dal.WorkStatusDal.GetAllList());
                ModelHandler<WorkStatusModel> modelHandler = new ModelHandler<WorkStatusModel>();
                if (result.Rows.Count > 0)
                {
                    var userReturn = modelHandler.FillModel(result);
                    return userReturn;
                }
                return null;
            }
            catch (Exception ex)
            {
                WriteLog.Writelog("WorkStatus.GetAllList=>" + ex.ToString());
                return null;
            }
        }

        public async Task<ActionResult> Delete(string WorkStatusId)
        {
            try
            {
                var result = await Task.Run(() => workStatusDal.Delete(WorkStatusId));
                if (result > 0)
                {
                    return new JsonResult(new
                    {
                        code = 20000,
                        message = "删除成功"
                    });
                }
                else
                {
                    return new JsonResult(new
                    {
                        code = 20002,
                        message = "删除失败"
                    });
                }
            }
            catch (Exception ex)
            {
                WriteLog.Writelog("WorkStatus.Delete=>" + ex.ToString());
                return new JsonResult(new
                {
                    code = 20001,
                    message = "发生错误"
                });
            }
        }
    }
}
